﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Web.Security;
using System.Text;
using Mytrip.Localization;

namespace Mytrip.Users.Controllers
{
    [Localization]
    public class AdminController : Controller
    {
        MembershipRepository db = new MembershipRepository();
        RoleRepository rb = new RoleRepository();
        // **************************************
        // URL: /Admin/Index/pageIndex?/pageSize?/sort?
        // **************************************
        public ActionResult Index(int? id, int? id2, string id3)
        {
            int total;
            int pageIndex = id ?? 1;
            int pageSize = id2 ?? 10;
            var model = db.AdminGetAllUsers((int)pageIndex,(int)pageSize,id3,out total);
            ViewData["total"] = total;           
            return View(model);
        }
        [HttpPost]
        public ActionResult Index(string search)
        {
            if(!String.IsNullOrEmpty(search))
            return RedirectToAction("Index", new { id=1,id2=10,id3=search});
            return RedirectToAction("Index");
        }
        // **************************************
        // URL: /Admin/Delete/UserName
        // **************************************
        public ActionResult Delete(string id)
        {            
            Membership.DeleteUser(id);
            return RedirectToAction("Index");
        }
        public ActionResult Details(string id)
        {
            ViewData["roles"] = new SelectList(rb._GetAllRoles(), "RoleName", "RoleName");
            var model = db._GetUserandEmail(id);
            return View(model);
        }
        // **************************************
        // URL: /Admin/IsApproved/UserName
        // **************************************
        public ActionResult IsApproved(string id)
        {
            db.UnlockUser(id);
            return RedirectToAction("Details", new { id});
        }
        // **************************************
        // URL: /Admin/IsUserInRole/UserName/RoleName
        // **************************************
        public ActionResult IsUserInRole(string id, string id2)
        {
            if(!String.IsNullOrEmpty(id)&&!String.IsNullOrEmpty(id2))
            rb._UnlockUserInRole(id, id2);
            return RedirectToAction("Details", new { id });
        }
        // **************************************
        // URL: /Admin/IndexRole/pageIndex/pageSize/sort
        // **************************************
        public ActionResult IndexRole(int id, int id2, string id3)
        {
            int total;
            var model = rb._GetAllRoles(id, id2, id3, out total);
            ViewData["total"] = total;
            return View(model);
        }
        [HttpPost]
        public ActionResult IndexRole(string search)
        {
            if (!String.IsNullOrEmpty(search))
                return RedirectToAction("IndexRole", new { id = 1, id2 = 10, id3 = search });
            return RedirectToAction("IndexRole", new { id = 1, id2 = 10, id3 = "RoleName" });
        }
        // **************************************
        // URL: /Admin/CreateRole/RoleName
        // **************************************
        public ActionResult CreateRole(string RoleName)
        {
            if(!String.IsNullOrEmpty(RoleName)&&rb._GetRole(RoleName)==null)
              Roles.CreateRole(RoleName);
            return RedirectToAction("IndexRole", new { id = 1, id2 = 10, id3 = "RoleName" });
        }
        // **************************************
        // URL: /Admin/DeleteRole/RoleName
        // **************************************
        public ActionResult DeleteRole(string id)
        {
            Roles.DeleteRole(id);
            return RedirectToAction("IndexRole", new { id = 1, id2 = 10, id3 = "RoleName" });
        }        
    }
}
